package demo1;

import java.util.Arrays;

public class Arrays_class_methods {
    public static void main(String[] args) {
        int[] arr1 = {1, 2, 3, 4, 5};
        //获取数组长度：arr.length
        //获取数组最大值：Arrays.stream(arr).max().getAsInt()
        System.out.println(Arrays.stream(arr1).max().getAsInt());

        //获取数组最小值：Arrays.stream(arr).min().getAsInt()
        System.out.println(Arrays.stream(arr1).min().getAsInt());

        //获取数组总和：Arrays.stream(arr).sum()
        System.out.println(Arrays.stream(arr1).sum());

        //获取数组平均值：Arrays.stream(arr).average().getAsDouble()
        System.out.println(Arrays.stream(arr1).average().getAsDouble());

        System.out.println("=========================");

        //数组的拷贝
        int[] arr2 = Arrays.copyOf(arr1, arr1.length);
        for(int i : arr2) {
            System.out.println(i);
        }

        System.out.println("=========================");

        //指定长度范围拷贝
        int[] arr3 = Arrays.copyOfRange(arr1, 1, 3);
        //后面两个参数是数组下标，左闭右开[1,3)

        //使用类方法打印数组
        System.out.println(Arrays.toString(arr3));

        System.out.println("=========================");

        //数组排序
        int[] arr4 = {10, 8, 4, 6, 2};
        Arrays.sort(arr4);
        System.out.println(Arrays.toString(arr4));
    }
}
